package com.nanjia.boot.product.spu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.brand.model.entity.BrandInfo;
import com.nanjia.boot.brand.model.query.BrandInfoQuery;
import com.nanjia.boot.brand.service.BrandInfoService;
import com.nanjia.boot.common.model.Option;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.product.category.model.query.ProductCategoryQuery;
import com.nanjia.boot.product.category.model.vo.ProductCategoryVO;
import com.nanjia.boot.product.category.service.ProductCategoryService;
import com.nanjia.boot.product.spec.group.model.entity.SpecGroup;
import com.nanjia.boot.product.spec.group.model.query.SpecGroupQuery;
import com.nanjia.boot.product.spec.group.service.SpecGroupService;
import com.nanjia.boot.product.spu.model.form.ConnectSpecGroup;
import com.nanjia.boot.product.spu.model.form.ProductSpuForm;
import com.nanjia.boot.product.spu.model.query.ProductSpuQuery;
import com.nanjia.boot.product.spu.model.vo.ProductSpuVO;
import com.nanjia.boot.product.spu.service.ProductSpuService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品基础信息(SPU)前端控制层
 *
 * @author 周洋
 * @since 2025-07-07 16:28
 */
@Tag(name = "商品基础信息(SPU)接口")
@RestController
@RequestMapping("/api/v1/product-spu")
@RequiredArgsConstructor
public class ProductSpuController  {

    private final ProductSpuService productSpuService;

    private final ProductCategoryService productCategoryService;

    private final BrandInfoService brandInfoService;

    private final SpecGroupService specGroupService;


    @Operation(summary = "商品基础信息(SPU)分页列表")
    @GetMapping("/page")
   //@PreAuthorize(@ss.hasPerm('spu:product-spu:query')")
    public PageResult<ProductSpuVO> getProductSpuPage(ProductSpuQuery queryParams ) {
        IPage<ProductSpuVO> result = productSpuService.getProductSpuPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "品牌列表")
    @GetMapping("/getBrandInfoVOList")
   //@PreAuthorize(@ss.hasPerm('spu:product-spu:query')")
    public Result<List<BrandInfo>> getBrandInfoVOList(BrandInfoQuery queryParams ) {
        List<BrandInfo> result = brandInfoService.getBrandInfoVOList(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "商品基础信息(SPU)分页列表")
    @GetMapping("/getProductCategoryList")
   //@PreAuthorize(@ss.hasPerm('spu:product-spu:query')")
    public Result<List<ProductCategoryVO>> getProductCategoryList(ProductCategoryQuery queryParams ) {
        List<ProductCategoryVO> result = productCategoryService.getProductCategoryList(queryParams);
        return Result.success(result);
    }



    @Operation(summary = "新增商品基础信息(SPU)")
    @PostMapping
   //@PreAuthorize(@ss.hasPerm('spu:product-spu:add')")
    public Result<Void> saveProductSpu(@RequestBody @Valid ProductSpuForm formData, HttpServletRequest request ) {
        boolean result = productSpuService.saveProductSpu(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取商品基础信息(SPU)表单数据")
    @GetMapping("/{id}/form")
   //@PreAuthorize(@ss.hasPerm('spu:product-spu:edit')")
    public Result<ProductSpuForm> getProductSpuForm(
        @Parameter(description = "商品基础信息(SPU)ID") @PathVariable Long id
    ) {
        ProductSpuForm formData = productSpuService.getProductSpuFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改商品基础信息(SPU)")
    @PutMapping(value = "/{id}")
   //@PreAuthorize(@ss.hasPerm('spu:product-spu:edit')")
    public Result<Void> updateProductSpu(
            @Parameter(description = "商品基础信息(SPU)ID") @PathVariable Long id,
            @RequestBody @Validated ProductSpuForm formData,HttpServletRequest request
    ) {
        boolean result = productSpuService.updateProductSpu(id, formData,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除商品基础信息(SPU)")
    @DeleteMapping("/{ids}")
   //@PreAuthorize(@ss.hasPerm('spu:product-spu:delete')")
    public Result<Void> deleteProductSpus(
        @Parameter(description = "商品基础信息(SPU)ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = productSpuService.deleteProductSpus(ids);
        return Result.judge(result);
    }

    @Operation(summary = "分类下拉列表")
    @GetMapping("/listProductCategoryOptions")
   //@PreAuthorize(@ss.hasPerm('spu:product-spu:query')")
    public Result<List<Option<Long>>> listProductCategoryOptions() {
        List<Option<Long>> list = productCategoryService.listProductCategoryOptions();
        return Result.success(list);
    }
}
